package org.hu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.hu.entity.dto.DishAddDTO;
import org.hu.entity.dto.DishPageQueryDTO;
import org.hu.entity.pojo.Dish;
import org.hu.entity.pojo.PageBean;
import org.hu.entity.vo.UpdateDishVO;
import org.hu.mapper.DishMapper;
import org.hu.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    // 新增菜品
    @Override
    public void addDish(DishAddDTO dishAddDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishAddDTO,dish);
        dish.setCreateTime(LocalDate.now());
        dish.setUpdateTime(LocalDate.now());
        dishMapper.insert(dish);
    }

    // 删除菜品
    public void deleteDish(Integer id) {
        dishMapper.deleteById(id);
    }



    // 修改菜品
    public void updateDish(UpdateDishVO updateDishVO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(updateDishVO,dish);
        dish.setUpdateTime(LocalDate.now());
        dishMapper.updateById(dish);
    }

    //条件查询菜品
    public PageBean listDish(DishPageQueryDTO dishPageQueryDTO) {
        // 获取分页的页码和容量
        PageHelper.startPage(dishPageQueryDTO.getPageNum(), dishPageQueryDTO.getPageSize());
        // 获取分页的数据
        List<Dish> dishList =dishMapper.selectList(dishPageQueryDTO);
        // 将数据封装成分页对象
        Page<Dish> page = (Page<Dish>) dishList;
        // 将分页对象转换为分页数据对象
        PageBean pageBean = new PageBean(page.getResult(),  page.getTotal());
        return pageBean;
    }
}
